// 封装过滤器
import moment from 'moment'
import Secondcode from './secondcode'

export default class Filters {
  /**
   * 二级代码过滤器
   * @param value
   * @param key
   * @returns {*}
   */
  static codeList = (value, key) => {
    if (!value) return ''
    if (!key) return value
    let data = {}
    const code = { ...Secondcode }
    Object.assign(data, code)
    // TODO:在此对二级代码对应字段特殊处理（大写）
    for (let i = 0; i < data[key].length; i++) {
      if (data[key][i]['VALUE'] === value) {
        value = data[key][i]['NAME']
        break
      }
    }
    return value
  }
  /**
   * 后台传回来的long日期转换成yyyy-mm-dd
   * @param value
   * @returns {*}
   */
  static longToString = (value) => {
    if (!value) return ''
    if (typeof value === 'string') return value
    value = JSON.stringify(value)
    const data = value.substring(0, 4) + '-' + value.substring(4, 6) + '-' + value.substring(6, 8)
    return data
  }
  /**
   * 补全金额
   * @param value
   * @returns {*}
   */
  static amountComplete = (value) => {
    if (!value) return '0.00'
    value = Math.round(parseFloat(value) * 100) / 100
    let xsd = value.toString().split('.')
    if (xsd.length === 1) {
      value = value.toString() + '.00'
      return value
    }
    if (xsd.length > 1) {
      if (xsd[1].length < 2) {
        value = value.toString() + '0'
      }
      return value
    }
  }

  // 通用自定义过滤器 saryz
  static filter = (value, key) => {
    return key && this[key] ? this[key](value) : value
  }
  // 根据身份证号码判断男女
  static sexIdcardFilter = value => {
    if (!value) return ''
    const data = value.substr(16, 1) % 2 === 1 ? '男' : '女'
    return data
  }

  static testFilter = (value) => {
    return '自定义过滤器' + value
  }
  // 身份证号脱敏('331082199708094687' 转换成 '33108219********87') 第8位开始替换8个
  static IDcardHide = value => {
    if (!value) return ''
    const data = value.replace(/(\d{8})\d{8}(\d*)/, '$1********$2')
    return data
  }

  // 手机号脱敏('13912345678' 转换成 '139****5678') 第3位开始替换4个
  static telHide = value => {
    if (!value) return ''
    let data = value.replace(/(\d{3})\d{4}(\d*)/, '$1****$2')
    return data
  }

  // 姓名脱敏(小明 转换成 *明   李小明 转换成 李*明   欧阳小明 转换成 欧**明)
  static nameHide = value => {
    if (!value) return ''
    if (value.length === 2) {
      return new Array(value.length).join('*') + value.substr(-1)
    } else {
      return (
        value.substr(0, 1) + new Array(value.length - 1).join('*') + value.substr(-1)
      )
    }
  }

  static mobileDentin = value => {
    if (!value) return ''
    const pat = /(\d{3})\d*(\d{4})/
    return value.replace(pat, '$1****$2')
  }
  static IDCardDentin = value => {
    if (!value) return ''
    const pat = /(\d{3})\d*(\d{4})/
    return value.replace(pat, '$1***********$2')
  }
  static IDCard2Sex = val => {
    return (parseInt(val.substr(16, 1)) % 2 === 1) ? '男' : '女'
  }
  static IDCard2Birth = val => {
    if (!val) return ''
    let birth = `${val.substr(6, 4)}-${val.substr(10, 2)}-${val.substr(12, 2)}`
    // let d = new Date(birth.replace(/-/g, '/'))
    // let chYear = d.getFullYear()
    // let chMounth = (d.getMonth() + 1) >= 1 && (d.getMonth() + 1) <= 9 ? `0${d.getMonth() + 1}` : d.getMonth() + 1
    // let chDate = (d.getDate() >= 0 && d.getDate() <= 9) ? `0${d.getDate()}` : d.getDate()
    // let exp = (chYear + '-' + chMounth + '-' + chDate)
    // if (birth !== exp) {
    //   return '****-**-**'
    // } else {
    return birth
    // }
  }
  static IDCard2NumberBirth = val => {
    if (!val) return ''
    return `${val.substr(6, 4)}${val.substr(10, 2)}${val.substr(12, 2)}`
  }
  static timestamp2Date = (val) => {
    return moment(val).format('YYYY-MM-DD')
  }
  // 200001 to 2000-01
  static yearMonthLongToString = val => {
    if (!val) return ''
    val = String(val)
    return val.substring(0, 4) + '年' + val.substring(4, 6) + '月'
  }
  static codeListSort = val => {
    if (val instanceof Array) {
      val.sort((a, b) => {
        return a.value - b.value
      })
    }
    return val
  }
}
